package org.graduation.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.graduation.feignapi.RBACClient;
import org.graduation.model.common.RBAC.Roles;
import org.graduation.model.common.ResponseResult;
import org.graduation.model.common.project.Project;
import org.graduation.model.common.project.ProjectUserTable;
import org.graduation.model.common.user.User;
import org.graduation.model.dtos.RBAC.AssignAndRevokeRoleDTO;
import org.graduation.project.mapper.MybatisPlusWrapperFactory;
import org.graduation.project.mapper.ProjectMapper;
import org.graduation.project.mapper.ProjectUserMapper;
import org.graduation.project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectUserMapper projectUserMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private RBACClient rbacClient;

    @Override
    public ResponseResult invite(Project project, User user) {
        ProjectUserTable projectUserTable = new ProjectUserTable(project, user);
        if (projectUserMapper.exists(MybatisPlusWrapperFactory.existRepeatInviteUser(projectUserTable))) {
            return ResponseResult.failed("不能重复邀请");
        }
        if (projectUserMapper.insert(projectUserTable) == 1) {
            rbacClient.assignRole(new AssignAndRevokeRoleDTO(project.getPId(), user.getUid(), Roles.TEAM_MEMBER.getRoleId()));
            return ResponseResult.success();
        } else {
            return ResponseResult.failed("邀请失败，请稍候再试");
        }
    }

    @Override
    public ResponseResult invite(Project project, User[] users) {
        ProjectUserTable projectUserTable = new ProjectUserTable();
        projectUserTable.setPid(project.getPId());
        for (User u : users) {
            projectUserTable.setUid(u.getUid());
            if (!projectUserMapper.exists(MybatisPlusWrapperFactory.existRepeatInviteUser(projectUserTable))) {
                projectUserMapper.insert(projectUserTable);
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult removeUser(Project project, User user) {
        ProjectUserTable projectUserTable = new ProjectUserTable(project, user);
        if (projectUserMapper.deleteById(projectUserTable) == 1) {
            return ResponseResult.success();
        } else {
            return ResponseResult.failed("删除失败，请稍候再试");
        }
    }

    @Override
    public ResponseResult removeUser(Project project, User[] users) {
        ProjectUserTable projectUserTable = new ProjectUserTable();
        projectUserTable.setPid(project.getPId());
        for (User u : users) {
            projectUserTable.setUid(u.getUid());
            projectUserMapper.deleteById(projectUserTable);
        }
        return ResponseResult.success();
    }

    @Override
    public List<Project> listByUser(Integer uid, Integer page) {
        LambdaQueryWrapper<ProjectUserTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(ProjectUserTable::getUid, uid)
        ;
        List<ProjectUserTable> projectUserTables = projectUserMapper.selectList(queryWrapper);
        List<Long> temp = new ArrayList<>();
        for(ProjectUserTable p: projectUserTables){
            temp.add(p.getPid());
        }
        List<Project> result = projectMapper.selectBatchIds(temp);
        if(result == null || result.isEmpty()){
            return new ArrayList<>();
        }
        return result;
    }
}
